﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using System.Text;
using System.Reflection;
using System.Data.SqlClient;


/// <summary>
///ObjectExtentions 的摘要说明
/// </summary>
public static class ObjectExtentions
{

    /// <summary>
    /// obj向string转换,防止数据库中的字段为null;
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static string ConvertObjToStr(this object o)
    {
        if (o == DBNull.Value || o == null)
        {
            return "";
        }
        else
        {
            return o.ToString().Trim();
        }
    }

    public static string DateTimeFormatString(this object o, string format)
    {
        if (o == null)
        {
            return "";
        }
        else
        {
            if (o is DateTime)
            {
                DateTime dt;
                if (DateTime.TryParse(o.ToString(), out dt))
                {
                    return dt.ToString(format);
                }
            }
            else if (o is DateTimeOffset)
            {
                DateTimeOffset dt_offset;
                if (DateTimeOffset.TryParse(o.ToString(), out dt_offset))
                {
                    return dt_offset.ToString(format);
                }
            }
            return o.ToString();
        }
    }

    public static List<T> DataTableToModelList<T>(this DataTable dt) where T : new()
    {
        if (dt == null || dt.Rows.Count == 0)
        {
            return null;
        }
        List<T> modelList = new List<T>();
        foreach (DataRow dr in dt.Rows)
        {
            T model = new T();
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                PropertyInfo pInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                if (pInfo != null && dr[i] != DBNull.Value)
                {
                    pInfo.SetValue(model, dr[i], null);
                }
            }
            modelList.Add(model);
        }
        return modelList;
    }

    public static T DataTableToModel<T>(this DataTable dt) where T : new()
    {
        var mode = dt.DataTableToModelList<T>();
        if (mode != null)
        {
            return mode[0];
        }
        return default(T);
    }
    public static string GetListBoxValue(this ListBox lb)
    {
        StringBuilder _strval = new StringBuilder();
        string strVal = "";
        int[] _arr = lb.GetSelectedIndices();
        for (int i = 0; i < _arr.Length; i++)
        {
            _strval.Append("'" + lb.Items[_arr[i]].Value + "',");
        }
        strVal = _strval.ToString();
        strVal = strVal.Substring(0, strVal.Length > 0 ? strVal.Length - 1 : 0);
        return strVal;
    }
    public static void GetParaValue(this  string str, char split, string ParaName, out string strSQL, out List<SqlParameter> Paras)
    {
        string strVal = "";
        List<SqlParameter> List = new List<SqlParameter>();

        if (str != "")
        {
            string[] arrT = str.Split(split);
            for (int i = 0; i < arrT.Length; i++)
            {
                strVal += "@" + ParaName + i + ",";
                List.Add(new SqlParameter("@" + ParaName + i, arrT[i]));
            }
            strVal = strVal.Substring(0, strVal.Length > 0 ? strVal.Length - 1 : 0);
        }

        strSQL = strVal;
        Paras = List;
    }
}